using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;
using Microsoft.Win32;

namespace WK35705XML
{
    public class UMPHelp
    {
        public static async Task<UMPs> OpenUMPFile()
        {
            var filechooser = new OpenFileDialog();
            filechooser.DefaultExt = ".xml";
            filechooser.Filter = "XML Files|*.xml";

            if (filechooser.ShowDialog() != true)
                return null;
            var fact = new TaskFactory<UMPs>();
            UMPs u;
            using (var s = filechooser.OpenFile())
            {
                u = (await fact.StartNew(() => { return Load(s); }));
            }
            return u;
        }

        public static async Task<SystemAnalyse> OpenUMPSystemFile()
        {
            var filechooser = new OpenFileDialog();
            filechooser.DefaultExt = ".xml";
            filechooser.Filter = "XML Files|*.xml";

            if (filechooser.ShowDialog() != true)
                return null;
            var fact = new TaskFactory<SystemAnalyse>();
            SystemAnalyse u;
            using (var s = filechooser.OpenFile())
            {
                u = (await fact.StartNew(() => { return LoadSystem(s); }));
            }
            return u;
        }

        public static UMPs Load(Stream s)
        {
            var ump = new UMPs();

            var serializer = new XmlSerializer(typeof(UMPs));
            ump = (UMPs)serializer.Deserialize(s);

            return ump;
        }

        public static SystemAnalyse LoadSystem(Stream s)
        {
            var ump = new SystemAnalyse();
            try
            {
                var serializer = new XmlSerializer(typeof (SystemAnalyse));

                ump = (SystemAnalyse) serializer.Deserialize(s);
            }
            catch (Exception)
            {
                MessageBox.Show("This was not a System Analyze File");
                return null;
            }
            return ump;
        }

        public static async Task SaveUMP(UMPs u)
        {
            var filechooser = new SaveFileDialog();
            filechooser.DefaultExt = ".xml";
            filechooser.Filter = "XML Files|*.xml";
            filechooser.ShowDialog();
            var fact = new TaskFactory();

            await fact.StartNew(() =>
            {
                using (var s = filechooser.OpenFile())
                {
                    var serializer = new XmlSerializer(typeof(UMPs));
                    serializer.Serialize(s, u);
                }
            });
        }
        public static async Task SaveUMPSystem(SystemAnalyse u)
        {
            var filechooser = new SaveFileDialog();
            filechooser.DefaultExt = ".xml";
            filechooser.Filter = "XML Files|*.xml";
            filechooser.ShowDialog();
            var fact = new TaskFactory();

            await fact.StartNew(() =>
            {
                using (var s = filechooser.OpenFile())
                {
                    var serializer = new XmlSerializer(typeof(SystemAnalyse));
                    serializer.Serialize(s, u);
                }
            });
        }
    }
}